package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.leanback.widget.OnChildViewHolderSelectedListener
import androidx.recyclerview.widget.RecyclerView
import com.compass.mobile.R
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentHomeCaseBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Classify
import com.compass.mobile.presenters.ClassifyTitlePresenter
import com.compass.mobile.ui.viewModel.NurseProjectViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 上门护理
 * Date:2024/5/20 21:26
 */
class HomeCareFragment : BaseVBFragment<FragmentHomeCaseBinding>() {

    private val nurseProjectViewModel: NurseProjectViewModel by viewModels()
    private var classifys: MutableList<Classify> = mutableListOf()
    private lateinit var caseItemFragment: CaseItemFragment

    override fun initView(view: View, savedInstanceState: Bundle?) {
        caseItemFragment = CaseItemFragment()
        displaySelectedFragment(caseItemFragment)
    }

    override fun initData() {
        nurseProjectViewModel.getClassify()
        nurseProjectViewModel.classifyLiveData.observe(this) {
            it?.let { list ->
                classifys = list
                initCaseTitle(classifys)
            }
        }

    }

    // 根据需要显示或替换Fragment
    private fun displaySelectedFragment(fragment: Fragment) {
        val fragmentTransaction: FragmentTransaction = childFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentChild, fragment)
        fragmentTransaction.commit()
    }

    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {

        }
    }


    // 保存上一个选中的位置 实现高亮显示效果
    var lastSelectedPosition: Int = -1
    private val onChildViewHolderSelectedListener: OnChildViewHolderSelectedListener = object :
        OnChildViewHolderSelectedListener() {
        override fun onChildViewHolderSelected(
            parent: RecyclerView?,
            childViewHolder: RecyclerView.ViewHolder?,
            position: Int,
            subposition: Int
        ) {
            childViewHolder?.itemView?.setBackgroundResource(R.drawable.case_focus)
            if (lastSelectedPosition!=-1){
                var lastViewHolder=parent?.findViewHolderForAdapterPosition(lastSelectedPosition)
                lastViewHolder?.itemView?.setBackgroundResource(R.drawable.case_title_selector)
            }
            lastSelectedPosition=position
            val item = classifys[position]
            caseItemFragment.setSelect(item.id)
        }
    }


    @SuppressLint("RestrictedApi")
    fun initCaseTitle(classify: MutableList<Classify>?) {
        mBinding?.gridCase?.verticalSpacing = dpToPx(10)
        val arrayObjectAdapter = ArrayObjectAdapter(ClassifyTitlePresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.gridCase?.setAdapter(itemBridgeAdapter)
        arrayObjectAdapter.addAll(0, classify)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
        mBinding?.gridCase?.setOnChildViewHolderSelectedListener(onChildViewHolderSelectedListener)
        mBinding?.gridCase?.requestFocus()

    }


}